<?php

/**
 * @author Thibaut Cromphaut, Gaïtano Boeraeve, Mathias Helin, Dimitry Dierickx
 * @copyright  Copyright (c) 2011 Artevelde University College Ghent 
 */

class Application_Model_EmailTypeMapper extends Ahs_Model_Mapper
{

    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Emails';
        $this->PropertiesMap = array
        (
            "Id"       => "emails_type_id",
            "Name"     => "emails_type_name",
        );
    }
    
    /**
     * Get all email types
     * @return Array(Application_Model_EmailType)
     */
    public function fetchAll()
    {        
        $table = $this->getDbTable();
        $select = $table->select();
        //$select->where(''.$this->getColumn('DeletedDate').' IS NULL');
        $rows = $table->fetchAll($select);
        return $this->_toEmailTypes($rows);
    }
    
    /**
     * Get a email type by its ID
     * 
     * @param integer $id
     * @return Application_Model_EmailType
     */
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array(''.$this->getColumn('Id').' = ?' => $id));
        return $this->_toEmailType($row);
    }
    
    /**
     * Save an email type
     *
     * @param Application_Model_EmailType $emailtype
     */
    public function save(Application_Model_EmailType $emailtype)
    {    
        $emailtype_id = $emailtype->Id;
        
        $data = $this->_getTableValues($emailtype, array("Id"));
        
        if(null == $emailtype_id)
        {
            // Insert and get the id
            $emailtype_id    = $this->getDbTable()->insert($data);
        }
        else
        {
            $this->getDbTable()->update($data, array(''.$this->getColumn('Id').' = ?' => $emailtype_id));
        }
        
        return $emailtype_id;
    }
    
    /**
     * Convert a row to object
     * 
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_EmailType
     */
    private function _toEmailType(Zend_Db_Table_Row_Abstract $row = null)
    {
        // Create our object
        $object = new Application_Model_EmailType();
        
        // Only go on if we actually have a row
        if($row)
        {
            // We have a propertiesmap, let's try to map it back to an object
            foreach($this->PropertiesMap as $key=>$column)
            {
                if(isset($row[$column]))
                    $object->$key      = $row[$column];
            }
        }

        return $object;
    }

    /**
     * Convert a rowset to an array of objects
     * 
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return Array(Application_Model_EmailType)
     */
    private function _toEmailTypes(Zend_Db_Table_Rowset_Abstract $rows = null)
    {
        $results = array();
        if ($rows)
        {
            foreach ($rows as $row)
            {
                $results[] = $this->_toEmailType($row);
            }
        }
        return $results;
    }
}